/*
 * @(#)BiomeType.java     14/08/02
 * 
 * Author             Rick Wilson
 * Copyright (c) 2014 Rick Wilson
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of 'Taareekah' nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */



package world;

//~--- non-JDK imports --------------------------------------------------------

import parameters.ParameterGroup;
import parameters.ParameterNode;

import util.Point;

//~--- JDK imports ------------------------------------------------------------

import java.io.Serializable;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 *
 * @author rick
 */
public class BiomeType implements Serializable {

    /**
     *
     */
    protected Set<Point>	all_points;

    /**
     *
     */
    protected String	name;

    /**
     *
     */
    protected String	desc;

    /** Field description */
    private Map<String, int[]>	param_ranges;

    /** Field description */
    private int[]	color;

    /** Field description */
    private UUID	id;

    /**
     *
     * @param group
     * @throws Exception
     */
    public BiomeType(ParameterGroup group) throws Exception {
	id	= UUID.randomUUID();
	setParameterGroup(group);
    }

    /**
     *
     * @return
     */
    public UUID getUUID() {
	return id;
    }

    /**
     * Method description
     *
     *
     * @param nodes
     *
     * @throws Exception
     */
    private void parseRangeNodes(Collection<ParameterNode> nodes) throws Exception {
	param_ranges	= new HashMap<>();

	for (ParameterNode p : nodes) {
	    if (!p.isGroup()) {
		continue;
	    }

	    ParameterGroup	g		= (ParameterGroup) p;
	    String		value_name	= g.getChild("valuename").getValueAsString();
	    float		min		= Float.parseFloat(g.getChild("min").getValueAsString());
	    float		max		= Float.parseFloat(g.getChild("max").getValueAsString());
	    int[]		min_max		= { (int) min, (int) max };

	    if (min_max[0] < 0) {
		min_max[0]	= 0;
	    }

	    if ((min_max[1] < 0) || (min_max[1] > 255)) {
		min_max[1]	= 255;
	    }

	    if (min_max[0] > min_max[1]) {
		int	temp	= min_max[1];

		min_max[1]	= min_max[0];
		min_max[0]	= temp;
	    }

	    param_ranges.put(value_name, min_max);
	}
    }

    /**
     *
     * @param group
     * @throws Exception
     */
    public void setParameterGroup(ParameterGroup group) throws Exception {
	color	= new int[3];
	name	= group.getChild("name").getValueAsString();

	String		c	= group.getChild("color").getValueAsString();
	String[]	splited	= c.split(",");

	color[0]	= Integer.parseInt(splited[0]);
	color[1]	= Integer.parseInt(splited[1]);
	color[2]	= Integer.parseInt(splited[2]);
	parseRangeNodes(group.getChildren());
    }

    /**
     *
     * @return
     */
    public int[] getColor() {
	return color;
    }

    /**
     *
     * @return
     */
    public String getName() {
	return name;
    }

    /**
     *
     * @param p
     */
    public void addPointWithType(Point p) {
	all_points.add(p);
    }

    /**
     *
     * @return
     */
    public Set<Point> getAllPointsWithType() {
	return all_points;
    }

    /**
     *
     * @param p
     * @return
     */
    public boolean hasPointWithType(Point p) {
	return all_points.contains(p);
    }

    /**
     *
     * @param name
     * @return
     * @throws Exception
     */
    public int getMinForParam(String name) throws Exception {
	return param_ranges.get(name)[0];
    }

    /**
     *
     * @param name
     * @return
     * @throws Exception
     */
    public int getMaxForParam(String name) throws Exception {
	return param_ranges.get(name)[1];
    }

    /**
     *
     * @param value_name
     * @param value
     * @return
     * @throws Exception
     */
    public boolean doesValueMeetCritera(String value_name, float value) throws Exception {
	if ((value < (float) getMinForParam(value_name)) || (value > (float) getMaxForParam(value_name))) {
	    return false;
	}

	return true;
    }

    /**
     *
     * @param values
     * @return
     * @throws Exception
     */
    public boolean doesPointMeetCriteria(Map<String, Float> values) throws Exception {
	for (Map.Entry<String, Float> entry : values.entrySet()) {
	    if (!doesValueMeetCritera(entry.getKey(), entry.getValue())) {
		return false;
	    }
	}

	return true;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
